package com.gperp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gperp.common.FaceSpot;
import com.gperp.common.GPResult;
import com.gperp.common.UserListResult;
import com.gperp.mapper.AsUserMapper;
import com.gperp.mapper.other.UserMapper;
import com.gperp.pojo.AsUser;
import com.gperp.pojo.AsUserExample;
import com.gperp.service.AsUserService;

@Service("asUserService")
public class AsUserServiceImpl implements AsUserService {
	@Autowired
	private AsUserMapper asUserMapper;
	@Resource
	private UserMapper userMapper;
	
	@Override
	public AsUser getUserLogin(String userCode,String userPassword) {
		AsUserExample example=new AsUserExample();		
		example.createCriteria().andUsercodeEqualTo(userCode);		
		List<AsUser> list=asUserMapper.selectByExample(example);
		if(list!=null&&list.size()>0){
			AsUser user = list.get(0);
			if (user.getUsercode()!=null) {
				if (user.getUserpassword().equals(userPassword)) {
					System.out.println("成功");
					return user;
				}else {
					System.out.println("用户名或者密码错误");
				}
			}		
		}else {
			System.out.println("用户不存在");
		}
		return null;
	}

	@Override
	public Map<String, Object> queryList(Integer pageNo, String username,Integer roleid, Integer isstart) {
		Map<String,Object> map = new HashMap<String,Object>();
		PageHelper.startPage(pageNo, 7);
		username = "%"+username+"%";
		PageInfo<UserListResult> pageInfo = new PageInfo<UserListResult>(userMapper.queryList(username,roleid,isstart));
		map.put("pageTotal", pageInfo.getTotal());
		map.put("pageList", pageInfo.getList());
		return map;
	}
	
	@Override
	public GPResult deleteById(String userIds) {
		if(userIds!=null&&!userIds.equals("")){
			try {
				for (String id : userIds.split(",")) {
					asUserMapper.deleteByPrimaryKey(Long.parseLong(id));
				}
				return GPResult.ok();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return GPResult.build(200, "NO");
	}

	@Override
	public GPResult checkCode(String usercode) {
		AsUserExample example = new AsUserExample();
		example.createCriteria().andUsercodeEqualTo(usercode);
		List<AsUser> user = asUserMapper.selectByExample(example);
		if(user!=null&&user.size()>0){
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public GPResult saveUser(AsUser user) {
		try {
			user.setCreatedby("admin");
			user.setCreationtime(new Date());
			user.setIsstart(1);
			asUserMapper.insertSelective(user);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public AsUser queryUserByID(Long id) {
		return asUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public GPResult updateUser(AsUser user) {
		try {
			asUserMapper.updateByPrimaryKeySelective(user);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public List<AsUser> queryUserAll() {
		return asUserMapper.selectByExample(null);
	}

	@Override
	public String checkFace(String img) {				//检查人脸
		return FaceSpot.existsUser(img,"Login").toString();
	}

	@Override
	public String saveFace(String img, String id) {		//保存人脸
		return FaceSpot.addUser2(img,"BASE64",id,"Login").toString();
	}

	@Override
	public String faceExists(String id) {
		return FaceSpot.searchUserInfo(id, "Login");
	}

}
